package cn.wolfcode.p2p.business.service.impl;
import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.mapper.BidMapper;
import cn.wolfcode.p2p.business.service.IBidService;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class BidServiceImpl implements IBidService {
    @Autowired
    private BidMapper bidMapper;

    @Override
    public BigDecimal getTotalBidAmountByBidUserIdAndBidRequestId(Long bidRequestId, Long id,Integer bidRequestState) {
        return bidMapper.selectTotalBidAmountByBidUserIdAndBidRequestId(bidRequestId,id,bidRequestState);
    }

    /**
     *
     * @param bidRequest 借款对象
     * @param amount 投资金额
     */
    @Override
    public void save(BidRequest bidRequest,BigDecimal amount) {
        Bid bid = new Bid();
        bid.setActualRate(bidRequest.getCurrentRate());
        bid.setAvailableAmount(amount);
        bid.setBidRequestId(bidRequest.getId());
        bid.setBidUser(UserContext.getLoginInfo());
        bid.setBidTime(new Date());
        bid.setBidRequestTitle(bidRequest.getTitle());
        bid.setBidRequestState(bidRequest.getBidRequestState());
        bidMapper.insert(bid);
    }

    /**
     *
     * @param bidRequestId 借款对象的id
     * @param bidrequestState 借款对象的状态
     */
    @Override
    public void updateByBidRequestIdAndState(Long bidRequestId, int bidrequestState) {
       bidMapper.updateByBidRequestIdAndState(bidRequestId,bidrequestState);
    }

    @Override
    public Bid getBid(Long bidId) {
        return bidMapper.selectByPrimaryKey(bidId);
    }

}
